package com.kuang.add;

/**
 * @author luomingliang
 * @date 2021/5/17 19:30
 */

import java.util.concurrent.CountDownLatch;

/**
 * 计数器
 */
public class CountDownLatchDemo {
    //CountDownLatch 一个线程等待N个线程执行完
    public static void main(String[] args) throws InterruptedException {
        //总数是6 必须要执行任务的时候，再使用
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <= 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"号玩家准备就绪");
                countDownLatch.countDown(); // -1
            }, String.valueOf(i)).start();
            System.out.println(Thread.currentThread().getName()+"玩家执行其他动作");
        }
        countDownLatch.await(); //等待计数器归零，再向下执行

        System.out.println("游戏开始！！");
    }
}
